package com.rongji.dfish.demo.comm.service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.rongji.dfish.base.Page;
import com.rongji.dfish.base.Utils;
import com.rongji.dfish.demo.entity.CommPost;
import com.rongji.dfish.demo.entity.CommPostPlate;
import com.rongji.dfish.demo.entity.CommPostReply;
import com.rongji.dfish.framework.service.BaseService;

@Service
public class CommService extends BaseService<CommPost, String>{
	
	/**
	 * 用来存放所属板块的缓存Map，如果是空的，就从数据库取，然后把结果存到MAP中。修改数据库内容了，就清空缓存，重新读一遍数据库，修改这个MAp的内容
	 */
	private static Map<String, CommPostPlate> plateCache=new LinkedHashMap<String, CommPostPlate>();
	/**
	 * 用来存放板块id和name的缓存List,数据从缓存Map中来
	 */
	private static List<String[]> POST_PLATE =new ArrayList<String[]>();
	public Map<String, CommPostPlate> getPlateCache() {
		if(Utils.isEmpty(plateCache)){
			List<CommPostPlate> plateList = (List<CommPostPlate>) pubCommonDAO.getQueryList("FROM CommPostPlate t  ORDER BY t.plateOrder ASC");
			for (CommPostPlate plate : plateList) {
				plateCache.put(plate.getPlateId(), plate);
			}
		}
		return plateCache;
	}
	public List<String[]> getPlateList() {
		if(Utils.isEmpty(POST_PLATE)){
			 for (Map.Entry<String, CommPostPlate> entry :getPlateCache().entrySet()) {
				POST_PLATE.add(new String[]{entry.getKey(),entry.getValue().getPlateName()});
			 }
		}
		return POST_PLATE;
	}
	public String getPlateName(String plateId) {
		Map<String, CommPostPlate> cache = getPlateCache();
		CommPostPlate plate = cache.get(plateId);
		if (plate == null) {
			return "全部";
		}
		return plate.getPlateName();
	}
	/**
	 * 查询不同板块的帖子
	 */
	public List<CommPost> findCommPosts(Page page,String plateId) {
		StringBuilder hql = new StringBuilder();
		List<Object> params = new ArrayList<Object>();
		hql.append("FROM CommPost t");
		
		if (Utils.notEmpty(plateId)) {
			hql.append(" WHERE t.postPlate = ?");
			params.add(plateId);
		}
		hql.append(" ORDER BY t.isTop ASC , t.postId DESC");
		@SuppressWarnings("unchecked")
        List<CommPost> result = (List<CommPost>) pubCommonDAO.getQueryList(hql.toString(),page,params.toArray());
		return result;
	}
	/**
	 * 查询我的帖子
	 */
	public List<CommPost> findMyCommPosts(Page page,String loginUserId) {
		StringBuilder hql = new StringBuilder();
		List<Object> params = new ArrayList<Object>();
		hql.append("FROM CommPost t WHERE t.postCreator = ?");
		params.add(loginUserId);
		hql.append(" ORDER BY t.postId DESC");
		@SuppressWarnings("unchecked")
		List<CommPost> result = (List<CommPost>) pubCommonDAO.getQueryList(hql.toString(),page,params.toArray());
		return result;
	}
	
	public CommPost getCommPost(String postId) {
		if (Utils.isEmpty(postId)) {
			return null;
		}
		List<CommPost> result = (List<CommPost>) getPubCommonDAO().getQueryList("FROM CommPost t WHERE t.postId=?", postId);
		if (Utils.notEmpty(result)) {
			return result.get(0);
		}
		return null;
	}
	
	/**
	 * 查询帖子的回复
	 */
	public List<CommPostReply> findCommReplys(Page page,String postId) {
		StringBuilder hql = new StringBuilder();
		List<Object> params = new ArrayList<Object>();
		hql.append("FROM CommPostReply t WHERE t.postId = ?");
		params.add(postId);
		hql.append(" ORDER BY t.replyId DESC");
		@SuppressWarnings("unchecked")
        List<CommPostReply> result = (List<CommPostReply>) pubCommonDAO.getQueryList(hql.toString(),page,params.toArray());
		return result;
	}
	
	public void saveReply(CommPostReply commPostReply) {
		pubCommonDAO.save(commPostReply);
	}
	
}
